using UnityEngine;
using UnityEngine.Events;

public class PlayerInput : MonoBehaviour {

    public Vector2Event OnMovementInput;
    public FloatEvent OnMovementVerticalMagnitude;
    public UnityEvent OnAttack;
    public FloatEvent OnShapeshiftInput;

    void Update() {
        UpdateMovementInput();
        UpdateAttackInput();
        UpdateShapeshiftInput();
    }

    void UpdateAttackInput() {
        if(Input.GetButtonDown("Fire1")) 
            if(OnAttack != null) OnAttack.Invoke();
    }

    void UpdateMovementInput() {
        Vector2 movement = Vector2.ClampMagnitude(new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), 1);

        if(OnMovementInput != null) OnMovementInput.Invoke(movement);
        if(OnMovementVerticalMagnitude != null) OnMovementVerticalMagnitude.Invoke(movement.magnitude);
    }

    void UpdateShapeshiftInput() {
        float axis = Input.GetAxis("Shapeshift");

        if(Input.GetButtonDown("Shapeshift")) {
            if(axis > 0) axis = 1;
            else if(axis < 0) axis = -1;

            if(OnShapeshiftInput != null) OnShapeshiftInput.Invoke(axis);
        }
    }
}